package com.blog.utils;
import io.jsonwebtoken.*;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.*;

public class JWTUtils {

    /**过期时间*/
    public static final long EXPIRE = 1000*60*60*24*7;
    /**密钥*/
    public static final String APP_SECRET = "GetSalt.getSalt()";
    /**根据昵称和角色名获取token*/
    public static String getJwtToken(String username,List roles,String pet,
                                     String userId,String photo,String description){
        return Jwts.builder()
                .setHeaderParam("typ", "JWT")
                .setHeaderParam("alg", "HS256")
                .setSubject("blog")   //存的字符串
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRE))
                .claim("username", username)//存用户名
                .claim("roles", roles)//存用户角色
                .claim("pet", pet)//存用户昵称
                .claim("userId", userId)//存用户id
                .claim("photo", photo)//存用户头像
                .claim("description",description)//存用户描述
                .signWith(SignatureAlgorithm.HS256, APP_SECRET)//签名
                .compact();
    }

    /**获得请求链
     *因为多处使用，所以抽成方法
     *
     */
    private static Claims getClaims(String jwtToken){
        Jws<Claims> claimsJws = null;
        try {
            claimsJws = Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(jwtToken);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
            return claimsJws.getBody();
    }
    /**
     * 判断token是否存在与有效
     * @param jwtToken token字符串
     * @return true有效，false无效
     */
    public static boolean checkToken(String username,String jwtToken) {
        if(StringUtils.isEmpty(jwtToken)) return false;
        Claims body = getClaims(jwtToken);
        String username1;
        if (body != null) {
            username1 = (String) body.get("username");
            return username1.equals(username);
        }
        return false;
    }
    public static boolean checkToken(String jwtToken) {
        if(StringUtils.isEmpty(jwtToken)) return false;
        Claims body = getClaims(jwtToken);
        if (!ObjectUtils.isEmpty(body)) {
            return true;
        }
        return false;
    }
    public static Map<String,Object> getInfo(String jwtToken){
        Map<String,Object> map=new HashMap<>();
        Claims body = getClaims(jwtToken);
        String username = (String) body.get("username");
        List roles = (List) body.get("roles");
        String photo= (String) body.get("photo");
        String description = (String) body.get("description");
        String userId= (String) body.get("userId");
        String pet = (String) body.get("pet");
        map.put("pet",pet);
        map.put("username",username);
        map.put("userId", userId);
        map.put("roles",roles);
        map.put("photo",photo);
        map.put("description", description);
        return map;
    }
    //刷新token
    public static String refreshJwtToken(String username,List roles,String pet,
                                         String userId,String photo,String description){
        return Jwts.builder()
                .setHeaderParam("typ", "JWT")
                .setHeaderParam("alg", "HS256")
                .setSubject("blog")   //存的字符串
                .setIssuedAt(new Date())
                .setExpiration(new Date())
                .claim("username", username)//存用户名
                .claim("roles", roles)//存用户角色
                .claim("pet", pet)//存用户昵称
                .claim("userId", userId)//存用户id
                .claim("photo", photo)//存用户头像
                .claim("description",description)//存用户描述
                .signWith(SignatureAlgorithm.HS256, APP_SECRET)//签名
                .compact();
    }
}
